package com.example.filesynsystem.util;

import com.example.filesynsystem.pojo.FileData;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class LocFileUtil {
    @Value("${s3.localPath}")
    private String localPath;

    //获取本地文件夹下所有文件信息
    public List<FileData> getFileData() {
        List<FileData> fileDataList = new ArrayList<>();
        File f = new File(localPath);
        if (!f.exists()) {
            System.out.println(localPath + "not exists");
            return null ;
        }

        File files[] = f.listFiles(); //用数组接收
        for (int i=0; i< files.length; i++) {
            File file = files[i];
            FileData fileData = new FileData();
            fileData.setKeyName(file.getName());
            Date lastModifiedTime = new Date(file.lastModified());
            fileData.setModifyTime(lastModifiedTime);
            fileData.setSize(file.length());
            fileDataList.add(fileData);
        }
        return fileDataList;
    }




}
